public class OrderPrintThread {

    private static Object LOCK = new Object();
    private static Integer count = 0;

    /**
     * 三个线程 顺序打印 1-100
     */
    public static void main(String[] args) {
        Run r0 = new Run(0,"A");
        Run r1 = new Run(1,"B");
        Run r2 = new Run(2,"C");
        new Thread(r0).start();
        new Thread(r1).start();
        new Thread(r2).start();
    }

    static class Run implements Runnable{
        public Run(Integer index,String name){
            this.index = index;
            this.name = name;
        }
        private Integer index;
        private String name;
        private Integer MAX = 100;

        @Override
        public void run() {
            while (count <= MAX){
                synchronized (LOCK){
                    if (count % 3 != index){
                        try {
                            LOCK.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    System.out.println(name +" "+ count);
                    count ++;
                    LOCK.notifyAll();
                }
            }
        }
    }


}
